package awk;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;

import util.Log4jUtil;

public class AWK {
    private static Logger logger = Logger.getLogger(AWK.class) ;
    private Vector<File> dateFile ;
    private String sourceFile ;
    private String outputFile ;
    public AWK(){
        dateFile = new Vector<File>() ;
    }
    public void setOutputFile(String outputFile) {
        this.outputFile = outputFile;
    }
    public void addFile(String file){
        this.dateFile.add(new File(file)) ;
    }
    public void setLog(String file){
        FileAppender fp = Log4jUtil.getFileAppender(file) ;
        logger.addAppender(fp) ;
        
    }
    public void handleFiles() throws Exception{
        logger.info("==>get jawk source from file: " + sourceFile ) ;

        AWKFactory fac = new AWKFactory() ;

        String cf = fac.setSourceFromFile(sourceFile) ;

        logger.info("==>get jawk source from file: \n-----\n" +cf +"\n----") ;
    	
    	LineHandler lr = fac.getAwk() ;
//        LineHandler lr = new TestJAWK();
        logger.info("==>already get the linehandler object, ready to handle file.") ;

    	if( outputFile != null ) lr.setLog(outputFile) ;
        logger.info("==>begin handle files\n\n") ;
        logger.info("==>call begin function.") ;
    	lr.begin() ;
        for (int i = 0; i < this.dateFile.size(); ++i) {

            File f = dateFile.get(i);
            logger.info("==>handling file " + f.getAbsolutePath());
            lr.handleFileByLine(f);
            logger.info("==>finish handle file " + f.getAbsolutePath());
        }
        logger.info("\n\n==>finish for all files") ;

        lr.end() ;
    }
    
    public String help(){
        String help="usage: \n"
                +"--data, -d, the data file you want to handle by jawk \n" 
                +"--source, -s, the jawk source file; \n" 
                +"--output, -o, the data you want to out put, you must use the info(String )/error(String), or use variable logger to write in the jawk function \n"
                +"--log, log file" ;
        return help ;
    }
    public void setSourceFile(String sourceFile) {
        this.sourceFile = sourceFile;
    }
    public static void main(String [] args) throws Exception{
        
    	AWK awk = new AWK() ;
    	if( args.length<=0){
    	    awk.logger.error(awk.help() );
    	    return ;
    	}
    	for( int i=0; i<args.length; ++i){
    	    String option = args[i] ;
    	    if(option.endsWith("--data") || option.equals("-d")){
    	        ++i ;
    	        awk.addFile(args[i]);
    	    }else if( option.endsWith("--source") || option.equals("-s")){
    	        ++i ;
                awk.setSourceFile(args[i]);
    	    }else if( option.endsWith("--output") || option.equals("-o")){
                ++i ;
                awk.setOutputFile(args[i]);
            }else if( option.endsWith("--log") ){
                ++i ;
                awk.setLog(args[i]);
            }else{
                throw new  Exception("did not know " + args[i]) ;
            }
    	}
//    	awk.addFile("C:\\Documents and Settings\\zniu\\Desktop\\tem\\cljmon.log"    			);
    	awk.handleFiles();
    }
}
